<?php
namespace App\Api\hvish\v1;
use App\Api\BaseController;
use Illuminate\Routing\Controller;
use App\Models\hvish\UserModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class UserController extends BaseController{
    public function __construct(UserModel $userModel){
        $this->user=$userModel;
    }
    function getRoom(){
        $d=$this->user->selectData();
        dd($d);
    }

    /**
     *用户注册
     * User 郭田
     * Date: ${DATE}
     * Url:
     * Results:
     */
    public function reg(Request $request){
        $creadentials = [
            'username'=>$request->get('username'),
            'nickname'=>$request->get('nickname'),
            'password'=>$request->get('password'),
        ];

        //验证参数
        $this->yan($creadentials);

        //添加数据
        $arr=[
            'username'=>$creadentials['username'],
            'password'=>$creadentials['password'],
            'nickname'=>$creadentials['nickname'],
        ];
        $res=$this->user->saveBy($arr);
        if($res){
            echo '注册成功';
        }else{
            echo '注册失败';
        }

    }

    /**
     * 测试事务
     * User 郭田
     * Date: ${DATE}
     * Url:
     * Results:
     */
    public function shiwu(Request $request){
//        $up=$this->user->updateBy(['mobile'=>'15201616023'],['uid'=>1]);
//        var_dump($up);exit;
        $creadentials = [
            'username'=>$request->get('username'),
            'nickname'=>$request->get('nickname'),
            'password'=>$request->get('password'),
        ];

        //验证参数
        $this->yan($creadentials);

        //添加数据
        $arr=[
            'username'=>$creadentials['username'],
            'password'=>$creadentials['password'],
            'nickname'=>$creadentials['nickname'],
        ];
        $r=$this->user->shiwu($arr);
        var_dump($r);
    }

    /**
     * 正则验证
     * User 郭田
     * Date: ${DATE}
     * Url:
     * Results:
     */
    public function yan(array $data){
        $rules=[
            'username'=>'required|min:6|max:20|unique:dy_user',
            'nickname'=>'required|min:6|max:20|',
            'password'=>'required|min:6|max:20|'
        ];
        $validator=app('validator')->make($data,$rules);
        if($validator->fails()){
            //exit(trans_code());
            echo '参数错误';
        }
        echo '验证正确';
    }
    function redisT(){
        $redis = Redis::connection('hvish');
        //$redis->set('la','la003');
        $set=$redis->hset('lara','arr',json_encode(array(1,2)));
        $get=$redis->hget('lara','arr');
        var_dump(json_decode($get),true);
    }

}
?>